package cn.iocoder.an.module.business.utils.strategy.sixFactory;

import cn.iocoder.an.module.business.dal.dataobject.filedaily.FileDailyDO;
import cn.iocoder.an.module.business.dal.mysql.reportconfig.ReportConfigMapper;
import cn.iocoder.an.module.business.dal.mysql.reportview.ReportViewMapper;
import cn.iocoder.an.module.business.service.filedaily.FileDailyService;
import cn.iocoder.an.module.business.utils.WeComMessage;
import cn.iocoder.an.module.business.utils.WeComRobotUtil;
import cn.iocoder.an.module.business.utils.strategy.ActionStrategy;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;

@Component("DunTouEquipmentStrategy")
public class DunTouEquipmentStrategy implements ActionStrategy {

    @Resource
    private FileDailyService fileDailyService;

    @Resource
    private WeComRobotUtil weComRobotUtil;


    @Value("${wecom.robot.default-webhook}") // ✅ 正确使用 Spring 的 @Value
    private String defaultWebhook;

    @Resource
    private ReportViewMapper reportViewMapper;

    @Resource
    private ReportConfigMapper reportConfigMapper;


    @Override
    public Object execute(Map<String, Object> params) {

        Long targetValue = (Long) params.get("targetValue");
        String name = (String) params.get("name");

        List<FileDailyDO> list = fileDailyService.getList();
        LocalDate itime = null;
        if (list != null && list.size() > 0) {
            itime = list.getFirst().getItime();
        }

        Long all = 0L;
        for (FileDailyDO fileDailyDO : list) {
            all = all + (fileDailyDO.getRunTime() != null ? fileDailyDO.getRunTime() : 0L);
        }

        all = all / 3600;

        if (all <= targetValue) {
//            var message = WeComMessage.text("测试消息", "17678970797");
//            boolean success = weComRobotUtil.send(defaultWebhook, message);
//            return null;

            String s = name + "[" + String.valueOf(itime) + "]平均运行时长:" + String.valueOf(all) + ",目标值:" + String.valueOf(targetValue);
            System.out.println(s);
            var message = WeComMessage.text(s);
            boolean success = weComRobotUtil.send(defaultWebhook, message);
            return null;
        }
        return null;
    }
}